﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace AAA.Forms.Components
{
    public partial class CheckItem : PictureBoxX
    {
        private string _strCheckedImage;
        private string _strUncheckImage;
        private Image _imgCheckedImage;
        private Image _imgUncheckImage;
        private bool _isChecked;

        public Image CheckedImage
        {
            get { return _imgCheckedImage; }
            set { _imgCheckedImage = value; }
        }        

        public Image UncheckImage
        {
            get { return _imgUncheckImage; }
            set { _imgUncheckImage = value; }
        }

        public bool IsChecked
        {
            get { return _isChecked; }
            set 
            {
                if (_isChecked == value)
                    return;
                _isChecked = value;
                ChangeImage((IsChecked ? CheckedImage : UncheckImage));    
            }
        }

        public CheckItem() : this("")
        {
        }

        public CheckItem(string strText)
            : this(strText, false)
        {
        }

        public CheckItem(string strText, bool isChecked)
        {
            InitializeComponent();
            Text = strText;
            IsChecked = isChecked;
            OffsetX = 30;
            DoubleBuffered = true;
            MouseClick += new System.Windows.Forms.MouseEventHandler(CheckItem_MouseClick);
        }

/*
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if ((Visible == false) || (Text == ""))
                return;
            PaintTitle(e.Graphics);
        }

        private new void PaintTitle(Graphics g)
        {
            SizeF fontSize;
            int iStartX;
            int iStartY;

            fontSize = g.MeasureString(Text, Font);

            iStartX = OffsetX;
            iStartY = (int)((Height - fontSize.Height) / 2);

            g.DrawString(Text, Font, new SolidBrush(ForeColor),
                         new Point(iStartX, iStartY));
        }
*/

        void CheckItem_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            IsChecked = !IsChecked;
        }

        public void LoadImage(string strUncheck, string strChecked)
        {
            try
            {
                UncheckImage = Image.FromFile(strUncheck);
                _strUncheckImage = strUncheck;
                Util.BitmapRegion.CreateControlRegion(this, UncheckImage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                CheckedImage = Image.FromFile(strChecked);
                _strCheckedImage = strChecked;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            ChangeImage((IsChecked ? CheckedImage : UncheckImage));
        }

        private void ChangeImage(Image image)
        {
            try
            {
                if (image != null)
                    Image = image;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

    }
}
